import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
# 基类
class Base():
    # 封装初始化方法
    def __init__(self,driver):
        self.driver=driver
    # 打开浏览器
    def open_url(self,url):
        self.driver.get(url)
    # 退出
    def quit(self):
        self.driver.quit()
    # 获取元素定位
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            str=time.strftime("%Y%m%d-%H%M%S")
            self.driver.save_screenshot(str+'baidu.png')
            print(e)
    # 点击
    def click(self,loc):
        self.find_element(loc).click()
    # 输入文本
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 窗口切换
    def window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # iframe处理
    def iframe(self,loc):
        self.driver.switch_to.frame(self.driver.find_element(*loc))
    # 弹窗处理
    def alert(self,loc):
        self.driver.switch_to.alert(self.driver.find_element(*loc))
    # 回车事件
    def enter(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    # text文本断言处理
    def text(self,loc):
        return self.find_element(loc).text
    # 滚动到底部
    def scrollTo(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')